/**
 * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.common.version.web;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.version.entity.AppVersion;
import cn.apestar.modules.common.version.service.AppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 更新Controller
 *
 * @author LVH
 * @version 2016-02-21
 */
@Controller
@RequestMapping(value = "${adminPath}/version/appVersion")
public class AppVersionController extends BaseController {

    @Autowired
    private AppVersionService appVersionService;

    @ModelAttribute
    public AppVersion get(@RequestParam(required = false) String id) {
        AppVersion entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = appVersionService.get(id);
        }
        if (entity == null) {
            entity = new AppVersion();
        }
        return entity;
    }

    @RequestMapping(value = { "list", "" })
    public String list(AppVersion appVersion, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<AppVersion> page = appVersionService.findPage(new Page<AppVersion>(request, response), appVersion);
        model.addAttribute("page", page);
        return "modules/version/appVersionList";
    }

    @RequestMapping(value = "form")
    public String form(AppVersion appVersion, Model model) {
        model.addAttribute("appVersion", appVersion);
        return "modules/version/appVersionForm";
    }

    @RequestMapping(value = "save")
    public String save(AppVersion appVersion, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, appVersion)) {
            return form(appVersion, model);
        }
        appVersionService.save(appVersion);
        addMessage(redirectAttributes, "保存更新成功");
        return "redirect:" + Global.getAdminPath() + "/version/appVersion/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(AppVersion appVersion, RedirectAttributes redirectAttributes) {
        appVersionService.delete(appVersion);
        addMessage(redirectAttributes, "删除更新成功");
        return "redirect:" + Global.getAdminPath() + "/version/appVersion/?repage";
    }

}